package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author fenge
 */
@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;

    @GetMapping({"/","/**"})
    public String toIndex(Model model, HttpServletRequest request){
        System.out.println(request.getHeader("userId"));
        System.out.println(request.getHeader("username"));
        //三级分类

        List<CategoryEntity> categoryEntityList = indexService.queryLvl1Categories();
        model.addAttribute("categories",categoryEntityList);
        return "index";
    }

    @GetMapping("index/cates/{pid}")
    @ResponseBody
    public ResponseVo<List<CategoryEntity>> queryLvl12CategoriesByPid(
            @PathVariable("pid")Long pid
    ){
        List<CategoryEntity> categoryEntities = indexService.queryLvl12CategoriesByPid(pid);
        return ResponseVo.ok(categoryEntities);
    }

    @GetMapping("index/test/lock")
    @ResponseBody
    public ResponseVo<Object> testLock(){
        indexService.testLock();
        return ResponseVo.ok();
    }
}
